package cn.yikaojiuguo.student.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.bean.GoldCharge;

/**
 * Created by cfan_yang on 2016/1/13.
 */
public class GoldChargeAdapter extends SimpleBaseAdapter<GoldCharge> {

    private int mChoose = -1;

    public GoldChargeAdapter(Context context, List<GoldCharge> list) {
        super(context, list);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_gold_charge,null);
            holder = new ViewHolder();
            holder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
            holder.iv_choose = (ImageView) convertView.findViewById(R.id.iv_choose);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        GoldCharge goldCharge = getItem(position);
        if(mChoose == position) {
            holder.tv_money.setTextColor(0xFFFFA103);
            holder.iv_choose.setVisibility(View.VISIBLE);
        } else {
            holder.iv_choose.setVisibility(View.INVISIBLE);
            holder.tv_money.setTextColor(Color.BLACK);
        }
        holder.tv_money.setText(goldCharge.gold + "金币(" + goldCharge.price + "元)");
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mChoose = position;
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    public int getChooseIndex() {
        return mChoose;
    }

    public GoldCharge getChooseItem() {
        if(mChoose>=0) {
            return getItem(mChoose);
        }
        return null;
    }

    static class ViewHolder {
        TextView tv_money;
        ImageView iv_choose;
    }
}
